What is is-https?
The is-https npm package is a simple utility to check if a request is made over HTTPS. It is particularly useful in server-side applications where you need to ensure secure connections.
What are is-https's main functionalities?
Check if request is HTTPS
This feature allows you to check if an incoming request is made over HTTPS. The code sample demonstrates a simple HTTP server that responds differently based on whether the request is HTTPS or not.
const isHttps = require('is-https');
const http = require('http');
const server = http.createServer((req, res) => {
if (isHttps(req)) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Request is HTTPS');
} else {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Request is not HTTPS');
}
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
Other packages similar to is-https
express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It includes middleware to check if a request is secure, similar to is-https, but offers a much broader range of functionalities.
helmet
Helmet helps you secure your Express apps by setting various HTTP headers. While it doesn't specifically check if a request is HTTPS, it can enforce HTTPS connections and provide additional security features.
is-https
Check if the given request is HTTPS
Usage
Install package:
yarn add is-https
npm install is-https
const isHTTPS = require('is-https')
import isHTTPS from 'is-https'
function isHTTPS(req: IncomingMessage, trustProxy: Boolean = true): Boolean | undefined
Behaviour
isHTTPS
function tries to use 2 different methods for HTTPS detection:
- Test if
x-forwarded-proto
header contains https
- Can be disabled by setting
trustProxy
argument to false
- Test if
req.connection.encrypted
is true
Returns either true
or false
based on checks or undefined
if no check was reliable.
TIP: If you want to redirect users from http
to https
, it is better using isHTTPS(req) === false
to avoid redirect loops.
Related
License
MIT